package cn.laixueit;

import org.junit.Test;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Demo {

    @Test
    public void t() throws Exception {

        Properties properties = new Properties();
        properties.setProperty("mail.host","smtp.163.com");
        properties.setProperty("mail.transport.protocol","smtp");

        Session session = Session.getInstance(properties);
        // message
        MimeMessage message = new MimeMessage(session);

        // 设置发件人
        message.setFrom(new InternetAddress("15618816293@163.com"));

        message.setSubject("某某发的标题");

        message.setText("neirong");

        message.setRecipients(Message.RecipientType.TO,"937220832@qq.com");

        //获取邮件发送的api
        Transport transport = session.getTransport();
        //邮件发送者的服务器地址，你的发送账号，授权码
        transport.connect("smtp.163.com","15618816293@163.com","WOYPWTAXHOHEFMTZ");

        // 发送邮件
        transport.sendMessage(message,message.getAllRecipients());

        transport.close();

    }
}
